/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MainFrm.h"
#include "testpgl.h"
#include "TestpglDoc.h"
#include "StaticDlg.h"
#include "PGLDemo.h"
#include <atlbase.h>
#include ".\mainfrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame
#ifdef PGL_NO_NEWMENU
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
#else
IMPLEMENT_DYNCREATE(CMainFrame, CNewFrameWnd)
#endif

#ifdef PGL_NO_NEWMENU
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
#else
BEGIN_MESSAGE_MAP(CMainFrame, CNewFrameWnd)
#endif
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_GRAPH_GENERATEGRAPH, OnGraphGenerateRegion)
	ON_COMMAND(ID_GRAPH_SHOW, OnGraphShow)
	ON_COMMAND(ID_EXAMPLES_SIMPLELINE, OnExamplesSimpleline)
	ON_COMMAND(ID_EXAMPLES_SIMPLELINEWITHLEVELOFDETAIL, OnExamplesSimplelinewithlevelofdetail)
	ON_COMMAND(ID_EXAMPLES_CUSTOMIZINGTHEAXIS, OnExamplesCustomizingtheaxis)
	ON_COMMAND(ID_EXAMPLES_SUBPLOTTING, OnExamplesSubplotting)
	ON_COMMAND(ID_EXAMPLES_TIMELABELS, OnExamplesTimelabels)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_COMMAND(ID_EXAMPLES_STACKEDLINES, OnExamplesStackedlines)
	ON_COMMAND(ID_EXAMPLES_LINEDASHING, OnExamplesLinedashing)
	ON_COMMAND(ID_EXAMPLES_LINECLOSINGFILLINDANDSNAPPING, OnExamplesLineclosingfillindandsnapping)
	ON_COMMAND(ID_EXAMPLES_MAPS, OnExamplesMaps)
	ON_COMMAND(ID_STATIC_SHOWDIALOGWITHSTATICPGLCONTROL, OnStaticShowdialogwithstaticpglcontrol)
	ON_COMMAND(ID_EXAMPLES_STOCKSHIGHLOWCLOSE, OnExamplesStockshighlowclose)
	ON_COMMAND(ID_EXAMPLES_SCATTERGROUPOFPOINTS, OnExamplesScattergroupofpoints)
	ON_COMMAND(ID_EXAMPLES_TRANSFORMATIONCONTAINERROTATIONANDSCALING, OnExamplesTransformationcontainerrotationandscaling)
	ON_COMMAND(ID_EXAMPLES_PATTERNSSPECIALIZEDCHARTING, OnExamplesPatternsspecializedcharting)
	ON_COMMAND(ID_EXAMPLES_TRIPLEBUFFERINGANIMATEDLINE, OnExamplesTriplebufferinganimatedline)
	ON_COMMAND(ID_GRAPH_SHOWINMODALDIALOG, OnGraphShowinmodaldialog)
	ON_COMMAND(ID_EXAMPLES_SATELLITEGOODIEROTATIONILLUSTRATION, OnExamplesSatellitegoodierotationillustration)
	ON_COMMAND(ID_EXAMPLES_ROTATEDREGIONS, OnExamplesRotatedregions)
	ON_COMMAND(ID_EXAMPLES_LINEINTERPOLATIONTYPES, OnExamplesLineinterpolationtypes)
	ON_COMMAND(ID_EXAMPLES_PIECHART, OnExamplesPiechart)
	ON_COMMAND(ID_EXAMPLES_SPLINES, OnExamplesSplines)
	ON_COMMAND(ID_EXAMPLES_BITMAPSANDIMAGESDRAWING, OnExamplesBitmapsandimagesdrawing)
	ON_COMMAND(ID_EXAMPLES_VECTORFIELD, OnExamplesVectorfield)
	ON_COMMAND(ID_EXAMPLES_DIFFERENTSFILLTYPESSOLIDLINEARGRADIENTTEXTURE, OnExamplesDifferentFills)
	ON_COMMAND(ID_EXAMPLES_MARKERS, OnExamplesMarkers)
	ON_COMMAND(ID_EXAMPLES_MULTIPLESCALINGONSAMECHART, OnExamplesMultiplescalingonsamechart)
	ON_COMMAND(ID_EXAMPLES_MANIPULATIONHETEROGENOUSDATATYPES, OnExamplesManipulationheterogenousdatatypes)
	ON_COMMAND(ID_EXAMPLES_SERIESMOVINGWITHTIME, OnExamplesSeriesmovingwithtime)
	ON_COMMAND(ID_EXAMPLES_BARGRAPHS, OnExamplesBargraphs)
	ON_COMMAND(ID_EXAMPLES_HISTOGRAM, OnExamplesHistogram)
	ON_COMMAND(ID_EXAMPLES_OUPUTTINGTOFILES, OnExamplesOuputtingtofiles)
	ON_COMMAND(ID_GDI_GETTINGINSTALLEDENCODERLIST, OnGdiGettinginstalledencoderlist)
	ON_COMMAND(ID_GDI_INSTALLEDDECODERLIST, OnGdiInstalleddecoderlist)
	ON_COMMAND(ID_EDIT_COPYASMETAFILE, OnEditCopyasmetafile)
	ON_COMMAND(ID_EDIT_COPY_ASBITMAP, OnEditCopyAsbitmap)
	ON_COMMAND(ID_MFCINTEGRATION_SHOWASBASICWINDOW, OnMfcintegrationShowasbasicwindow)
	ON_COMMAND(ID_EXPORTING_SVGTOSTRING, OnExportingSvgtostring)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_PNG, OnExportingOutputtingallexamplestoPng)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_JPEG, OnExportingOutputtingallexamplestoJpeg)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_EPS, OnExportingOutputtingallexamplestoEps)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_EMF, OnExportingOutputtingallexamplestoEmf)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_BMP, OnExportingOutputtingallexamplestoBmp)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_WMF, OnExportingOutputtingallexamplestoWmf)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_TIFF, OnExportingOutputtingallexamplestoTiff)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_SVG, OnExportingOutputtingallexamplestoSvg)
	ON_COMMAND(ID_MISC_MEMORYLEAKDETECTION, OnMiscMemoryleakdetection)
	ON_COMMAND(ID_EXAMPLES_ERRORBARS, OnExamplesErrorbars)
	ON_COMMAND(ID_MISC_OBJECTSINMEMORY, OnMiscObjectsinmemory)
	ON_COMMAND(ID_MISC_LOGGINGENABLED, OnMiscLoggingenabled)
	ON_UPDATE_COMMAND_UI(ID_MISC_LOGGINGENABLED, OnUpdateMiscLoggingenabled)
	ON_COMMAND(ID_EXPORTING_EMFTOISTREAM, OnExportingEmftoistream)
	ON_COMMAND(ID_EXPORTING_OUTPUTTINGALLEXAMPLESTO_SVGZ, OnExportingOutputtingallexamplestoSvgz)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EXAMPLES_HISTOGRAM2, OnExamplesHistogram2)
	ON_COMMAND(ID_EXAMPLES_TEXTANNOTATIONSANDBOXES, OnExamplesTextannotationsandboxes)
	ON_COMMAND(ID_EXAMPLES_SICKDATAPLOTTINGEMPTYLINESNANETC, OnExamplesSickdataplottingemptylinesnanetc)
	ON_COMMAND(ID_SPECIAL_LOWLEVELANIMATION, OnSpecialLowlevelanimation)
	//}}AFX_MSG_MAP
	ON_WM_SIZE()
	ON_COMMAND(ID_CAPI_TESTMEMORYBITMAP, OnCapiTestmemorybitmap)
	ON_COMMAND(ID_MOREEXAMPLES_STACKEDLINES, OnMoreexamplesStackedlines)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	CoInitialize(NULL);
}

CMainFrame::~CMainFrame()
{
	CoUninitialize();
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
#ifdef PGL_NO_NEWMENU
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
#else
	if (CNewFrameWnd::OnCreate(lpCreateStruct) == -1)
#endif
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndToolBarExamples.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBarExamples.LoadToolBar(IDR_TOOLBAR_EXAMPLE))
	{
		TRACE0("Failed to create toolbar Examples\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

#ifndef PGL_NO_NEWMENU
	m_DefaultNewMenu.LoadToolBar(IDR_MAINFRAME);
	m_DefaultNewMenu.LoadToolBar(IDR_TOOLBAR_EXAMPLE);
#endif

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	m_wndToolBarExamples.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);

	DockControlBar(&m_wndToolBar);
	DockControlBar(&m_wndToolBarExamples, AFX_IDW_DOCKBAR_LEFT);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
#ifdef PGL_NO_NEWMENU
	if( !CFrameWnd::PreCreateWindow(cs) )
#else
	if( !CNewFrameWnd::PreCreateWindow(cs) )
#endif
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
#ifdef PGL_NO_NEWMENU
	CFrameWnd::AssertValid();
#else
	CNewFrameWnd::AssertValid();
#endif
}

void CMainFrame::Dump(CDumpContext& dc) const
{
#ifdef PGL_NO_NEWMENU
	CFrameWnd::Dump(dc);
#else
	CNewFrameWnd::Dump(dc);
#endif
}

#endif //_DEBUG


void CMainFrame::OnGraphGenerateRegion() 
{
	CTestpglDoc* pDoc = (CTestpglDoc*)GetActiveDocument();
	CPGLRegionPtr  pRegion = ((CTestpglApp*)AfxGetApp())->GenerateRegion(); 
	CPGLView* pView = (CPGLView*)GetActiveView();
	ASSERT(pView);

	pRegion->ZoomAll(pView->GetGfxInterface(), true);

	pDoc->SetRegion(pRegion);

	pView->SetDirty();
}

void CMainFrame::OnGraphShow() 
{
	CPGLRegionPtr  pRegion = ((CTestpglApp*)AfxGetApp())->GenerateRegion();

	//Create a dialog box and display the plot:
	m_pglDlgManager.Create(pRegion);

}

void CMainFrame::OnGraphShowinmodaldialog() 
{
	CPGLRegionPtr  pRegion = ((CTestpglApp*)AfxGetApp())->GenerateRegion();

	//Create a dialog box and display the plot:
	CPGLDialog graphDlg(this, pRegion);
	graphDlg.DoModal();
}


void CMainFrame::OnExamplesSimpleline() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);
	
	CPGLDemo::GetExampleSimpleLine(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);
	graphdlg.DoModal();
}



void CMainFrame::OnExamplesTextannotationsandboxes() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);
	
	CPGLDemo::GetExampleTextAnnotations(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);
	graphdlg.DoModal();	
}
void CMainFrame::OnExamplesSimplelinewithlevelofdetail() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);
	CPGLDemo::GetExampleLineLOD(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);
}

void CMainFrame::OnExamplesCustomizingtheaxis() 
{
	using namespace gfxinterface;

	CPGLRegionPtr  pRegion(new CPGLRegion);
	CPGLDemo::GetExampleAxis(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion, CPGLDialog::OptionNoZoomAll /* Disable auto-zoom... */ );
	// change scaling...
	pRegion->GetAxe()->SetLimits( CPGLRectD( /*left */0.5, /*bottom*/-1e5, /*right*/0.9 , /*top*/1e5), pGraphDlg->GetGfxInterface());

	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);

}

void CMainFrame::OnExamplesSubplotting() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);

	CPGLDemo::GetExampleSubplot(pRegion);
	
	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);
}

void CMainFrame::OnExamplesTimelabels() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);

	CPGLDemo::GetExampleTimeLabel(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);
}

void CMainFrame::OnHelpAbout() 
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();	
}


void CMainFrame::OnExamplesStackedlines() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);

	CPGLDemo::GetExampleStackedLines(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);
}


void CMainFrame::OnMoreexamplesStackedlines()
{
	CPGLRegionPtr  pRegion(new CPGLRegion);

	CPGLDemo::GetExampleStackedLines2(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);
}

void CMainFrame::OnExamplesLinedashing() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);
	
	CPGLDemo::GetExampleDashing(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);
}

void CMainFrame::OnExamplesLineclosingfillindandsnapping() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);
	
	CPGLDemo::GetExampleClosing(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();	
}


void CMainFrame::OnExamplesMaps() 
{
	CPGLRegionPtr  pRegion(new CPGLRegion);
	
	CPGLDemo::GetExampleMaps(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}

void CMainFrame::OnStaticShowdialogwithstaticpglcontrol() 
{
	CStaticDlg dlg;
	
	dlg.DoModal();
}


void CMainFrame::OnExamplesStockshighlowclose() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
	
	CPGLDemo::GetExampleStock(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	gfxinterface::CGfxInterface& gfx=graphdlg.GetGfxInterface();
	pRegion->ZoomAll(graphdlg.GetGfxInterface());
	graphdlg.DoModal();		
	
}

void CMainFrame::OnExamplesScattergroupofpoints() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
	
	CPGLDemo::GetExampleScatter(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
	
}

void CMainFrame::OnExamplesTransformationcontainerrotationandscaling() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
	
	CPGLDemo::GetExampleContainer(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	gfxinterface::CGfxInterface& gfx=graphdlg.GetGfxInterface();
	pRegion->ZoomAll(graphdlg.GetGfxInterface());
	graphdlg.DoModal();		
	
}

void CMainFrame::OnExamplesPatternsspecializedcharting() 
{
	using namespace std;
	using namespace Gdiplus;
	using namespace gfxinterface;

	CPGLRegionPtr  pRegion (new CPGLRegion);
	pRegion->SetName(_T("Patterns"));

	CPGLScatterPtr pScatter(new CPGLScatter);
	CPGLVectorFPtr pX(new CPGLVectorF);
	CPGLVectorFPtr pY(new CPGLVectorF);
	CPGLVectorFPtr pValues(new CPGLVectorF);

	vector<float>& vX=pX->GetV();
	vector<float>& vY=pY->GetV();
	vector<float>& vValues=pValues->GetV();


	pScatter->SetDatas(pX,pY,pValues);

	pScatter->SetMinValue(0);
	pScatter->SetMaxValue(1);
	pScatter->SetColorMap(CPGLUtility::ColorMapJet);
	pScatter->SetMarkerWidth(1);

	vX.push_back(0); vY.push_back(0); vValues.push_back(0);
	vX.push_back(1); vY.push_back(1); vValues.push_back(1);
	pScatter->SetDataFlushing(true);

	pRegion->AddObject(pScatter);
	pRegion->GetAxe()->SetTitle( _T("Pattern generation and back frame manipulation."));

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion, CPGLDialog::OptionNoZoomAll);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);

	// zooming
	pRegion->ZoomRegion( pGraphDlg->GetGfxInterface(), CPGLRectD::GetNormBox());

	// peek and pump...
	CPGLUtility::PeekAndPump();

	// add and modify data...
	float x,y,v;
	srand( (unsigned)time( NULL ) );


	const UINT nPoints = 1000000000;
	UINT n=0;
	for (UINT i=0;i< nPoints;i++)
	{
		if (m_pglDlgManager.IsEmpty())
			return;

		x=(float)(pow(rand()/(double)RAND_MAX,2));
		y=(float)(rand()/(double)RAND_MAX);
		v=(float)(x/2.0+y/2.0+( rand()/(double)RAND_MAX-0.5 )*0.3);

		vX.push_back(x); vY.push_back(y); vValues.push_back(v);

		if (i%500 == 0)
		{
			// applying messages
			CPGLUtility::PeekAndPump();
			// check if dialog is still existing
			if ( !m_pglDlgManager.IsInList(pGraphDlg) )
				return;

			//! getting gfx intercae
			CGDIpInterface& gfx = pGraphDlg->GetGfxInterface();
			// getting graphics object associated to memory bitmap
			Graphics* pMemGraphics = pGraphDlg->GetGDIpHelper().GetMemGraphics();
			//! getting client device context of dialog window
			CDC* pDC = pGraphDlg->GetDC();
			if (pMemGraphics && pDC)
			{
				gfx.SetGraphics(pMemGraphics);
				gfx.BeginPaint(pDC->m_hDC);
					// push clipping stuff....
					pRegion->PushWorldClipBBox(gfx);
					pScatter->PlotGfx(gfx);
					pRegion->PopWorldClipBBox(gfx);
				gfx.EndPaint();
				// redraw screen
				pGraphDlg->SetCacheDirty();
				// release dc
				pGraphDlg->ReleaseDC(pDC);
				// pumping messages...
				CPGLUtility::PeekAndPump();
			}
			else
				return;
		}
	}
}

void CMainFrame::OnExamplesTriplebufferinganimatedline() 
{
	using namespace Gdiplus;
	using namespace gfxinterface;

	UINT k,i,n=100;
	CPGLColor col;
	CPGLRegionPtr  pRegion (new CPGLRegion);
	pRegion->SetName(_T("Animation, layers and triple buffering"));

	CPGLLine2DPtr pLine( new CPGLLine2D);

	std::vector<double> vX(n);
	std::vector<double> vY(n);

	for (i=0;i<n;i++)
	{
		vX[i]=i/(double)(n-1);
		vY[i]=sin(vX[i]*PGL_PI*2);
	}
	
	pLine->SetDatas(vX,vY);

	pLine->SetLayer(LayerFront);
	pLine->SetLineWidth(2);
	pLine->SetFilled(true);
	pLine->SetSnapBottom(true);
	CPGLLinearGradientBrushPtr pBrush(new CPGLLinearGradientBrush( CPGLColor(1,.5,0,0.5f), CPGLColor(0,.5,1,0.8f),45));
	pLine->SetBrush(pBrush);


	pRegion->AddObject(pLine);
	CPGLAxe2D* pAxis = pRegion->GetAxe();
	pAxis->SetTitle( _T("Animated dialog using triple buffering (no antialiasing)."));
	pAxis->GetBottomNumber().SetLayer( LayerFront);
	pAxis->GetBottomNumber().SetAlign(TextAlignmentLeft);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion, CPGLDialog::OptionTripleBuffering);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);

	pGraphDlg->GetGfxInterface().SetRenderOption( CGfxInterface::RenderOptionNoAntiAlias 
		| CGfxInterface::RenderOptionNoTextAntiAlias 
		| CGfxInterface::RenderOptionNoBiCubicInterpolation ); 

	// peek and pump...
	CPGLUtility::PeekAndPump();

	CPGLData* p=&(*pLine->GetY());
	ASSERT(p->IsKindOf(_T("CPGLVectorD")));
	CPGLVectorD::VectorT& vY2 = ((CPGLVectorD*)p)->GetV();

	k=0;
	while(1)
	{
		k++;
		if (m_pglDlgManager.IsEmpty())
			return;

		for (i=0;i<n;i++)
		{
			vY2[i]=sin((vX[i]+k/100.0)*PGL_PI*2);
		}
		CPGLUtility::ColorMap(CPGLUtility::ColorMapHsv, 0.5+sin(k/50.0*PGL_PI*2)/2.0, col);
		pLine->SetColor(col);
		CPGLUtility::ColorMap(CPGLUtility::ColorMapJet, 0.5+sin(k/100.0*PGL_PI*2)/2.0, col);
		pAxis->GetBottomNumber().SetAngle(  -45+45*(float)sin((float)(k/100.0*PGL_PI)) );
		pAxis->GetBottomNumber().SetColor(col);

		// applying messages
		CPGLUtility::PeekAndPump();

		// check if dialog is still existing
		if ( !m_pglDlgManager.IsInList(pGraphDlg) )
			return;

		// redraw screen
		pGraphDlg->SetFrontDirty();
	}
}

void CMainFrame::OnExamplesSatellitegoodierotationillustration() 
{
	using namespace gfxinterface;
	using namespace std;

	CString str;
	CPGLColor col;

	CPGLRegionPtr  pRegion (new CPGLRegion);
	CPGLAxe2D* pAxis = pRegion->GetAxe();
	pRegion->SetName(_T("Region rotation"));

	// setting axis
	pAxis->GetBottomLabel().Hide();
	pAxis->GetLeftLabel().Hide();
	pAxis->GetTopLabel().SetLayer(LayerFront);
	pAxis->GetHorGrid().SetDashPattern(PGL_LINE_CONT);
	pAxis->GetVerGrid().SetDashPattern(PGL_LINE_CONT);
	CPGLLinearGradientBrushPtr brush(new CPGLLinearGradientBrush(CPGLColor(.8f,.8f,.8f), CPGLColor(1,1,1),90));
	pAxis->SetBrush(brush);

	// creating container
	CPGLContainerPtr pContainer(new CPGLContainer);
	pContainer->SetLayer(LayerFront);
	pContainer->PushRotation(0, CPGLPointD(250,250));
	pContainer->PushScaling(CPGLPointD(0.6,1), CPGLPointD(100,100));

	int i,n =100;
	vector<double> vX(4), vY(4);
	for (i=0;i<n;i++)
	{
		CPGLLine2DPtr pSquare(new CPGLLine2D);
		pSquare->SetFilled(true);
		pSquare->SetOpen(false);
		CPGLUtility::ColorMap(CPGLUtility::ColorMapJet, i/(double)n, col);
		pSquare->SetSolidFillColor(col);
		pSquare->SetLineWidth(1.8f);
		pSquare->SetLayer(LayerFront);

		
		vX[0]=vX[3]=(i%10)*50; vX[1]=vX[2]=((i%10)+1)*50;
		vY[0]=vY[1]=(2*i/10)*50; vY[2]=vY[3]=(2*i/10+1)*50;
		pSquare->SetDatas(vX,vY);

		pContainer->AddObjectHead(pSquare);
	}
	pRegion->AddObject(pContainer);

	//Create a dialog box and display the plot:
	m_pglDlgManager.RemoveHidden();
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion, CPGLDialog::OptionNoZoomAll | CPGLDialog::OptionTripleBuffering);
	m_pglDlgManager.AddHead(pGraphDlg);
	// zooming
	pRegion->ZoomRegion( pGraphDlg->GetGfxInterface(), CPGLRectD(0,0,500,500));	

	// peek and pump...
	CPGLUtility::PeekAndPump();

	int k=0;
	while(1)
	{
		k++;
		k%=360;
		if (m_pglDlgManager.IsEmpty())
			return;

		pContainer->PopTransformation();
		pContainer->PopTransformation();
		pContainer->PushRotation(k/360.0*PGL_PI*2, CPGLPointD(250,250));
		pContainer->PushScaling(CPGLPointD(0.6+0.4*sin(k/90.0*PGL_PI*2), 0.6+0.4*cos(sin(k/90.0*PGL_PI*2)) ), CPGLPointD(100,100));

		str.Format(_T("Angle: %3g, Scaling: %1.2g,%1.2g"), k*1.0, 0.6+0.4*sin(k/180.0*PGL_PI*2), 0.6+0.4*cos(sin(k/180.0*PGL_PI*2)));
		pAxis->SetTitle(str);

		// applying messages
		CPGLUtility::PeekAndPump();

		// check if dialog is still existing
		if ( !m_pglDlgManager.IsInList(pGraphDlg) )
			return;

		// redraw screen
		pGraphDlg->SetFrontDirty();
	}
}

void CMainFrame::OnExamplesRotatedregions() 
{
	using namespace gfxinterface;
	using namespace std;

	CPGLColor col;
	col.SetAlpha(0.2f);

	CPGLRegionPtr  pRegion (new CPGLRegion);
	CPGLAxe2D* pAxis = pRegion->GetAxe();

	// setting axis
	pAxis->GetBottomLabel().Hide();
	pAxis->GetLeftLabel().Hide();
	pAxis->GetTopLabel().SetLayer(LayerFront);
	pAxis->GetHorGrid().SetDashPattern(PGL_LINE_CONT);
	pAxis->GetVerGrid().SetDashPattern(PGL_LINE_CONT);
	CPGLLinearGradientBrushPtr brush(new CPGLLinearGradientBrush(CPGLColor(.8f,.8f,.8f), CPGLColor(1,1,1),90));
	pAxis->SetBrush(brush);
	pAxis->SetTitle(_T("Parent region"));


	CPGLRegionPtr  pChildRegion = pRegion->AddRegion();
	pChildRegion->SetNormBBox(0.15f,0.15f,0.85f,0.85f);
	pChildRegion->SetAngle(PGL_PI/4.0);
	pChildRegion->SetSolidFillColor(1,1,1,0);
	pChildRegion->GetAxe()->SetSolidFillColor(1,1,1,0);
	pChildRegion->GetAxe()->SetTitle(_T("Child (rotated) region"));
	pChildRegion->GetAxe()->GetBottomLabel().Hide();
	pChildRegion->GetAxe()->GetLeftLabel().Hide();

	int i,n =99;
	vector<double> vX(4), vY(4);
	for (i=0;i<n;i+=2)
	{
		CPGLLine2DPtr pSquare(new CPGLLine2D);
		pSquare->SetFilled(true);
		pSquare->SetOpen(false);
		CPGLUtility::ColorMap(CPGLUtility::ColorMapJet, i/(double)n, col);
		pSquare->SetSolidFillColor(col);
		pSquare->SetLineWidth(1.8f);
		pSquare->SetLayer(LayerFront);

		
		vX[0]=vX[3]=(i%10)*50; vX[1]=vX[2]=((i%10)+1)*50;
		vY[0]=vY[1]=(2*i/10)*50; vY[2]=vY[3]=(2*i/10+1)*50;
		pSquare->SetDatas(vX,vY);

		pChildRegion->AddObject(pSquare);
	}
	
	//Create a dialog box and display the plot:
	m_pglDlgManager.RemoveHidden();
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion);
	m_pglDlgManager.AddHead(pGraphDlg);
}

void CMainFrame::OnExamplesLineinterpolationtypes() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
	
	CPGLDemo::GetExampleLineInterpolation(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();	
}


void CMainFrame::OnExamplesPiechart() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
	
	CPGLDemo::GetExamplePie(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}

void CMainFrame::OnExamplesSplines() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
	
	CPGLDemo::GetExampleSplines(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);
	graphdlg.DoModal();		
}

void CMainFrame::OnExamplesBitmapsandimagesdrawing() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleBitmap(pRegion, _T("testImage.jpg"));

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}

void CMainFrame::OnExamplesVectorfield() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleVectorField(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}

void CMainFrame::OnExamplesDifferentFills() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleDifferentFills(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}


void CMainFrame::OnExamplesMarkers() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleMarkers(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}


void CMainFrame::OnExamplesMultiplescalingonsamechart() 
{
	using namespace Gdiplus;
	using namespace gfxinterface;

	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleMultipleAxis(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		

	CPGLArchive ar(pRegion);

/*
	CPGLPointU dim = CPGLPointU(640, 480);
	CComPtr<IStream>    pStream;
	// create a new stream
	HRESULT hr = CreateStreamOnHGlobal( 
		NULL,         // HGLOBAL hGlobal
		TRUE,         // BOOL fDeleteOnRelease
		&pStream);  // LPSTREAM* ppstm

	bool bSuccess=ar.ExportToStream(CPGLArchive::FileFormatEMF,pStream, dim, NULL, true);

	if(bSuccess)
		AfxMessageBox(_T("Success"));
	else
		AfxMessageBox(_T("Failed"));
		*/

}



void CMainFrame::OnExamplesManipulationheterogenousdatatypes() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleDataHandling(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}

void CMainFrame::OnExamplesSeriesmovingwithtime() 
{
	using namespace Gdiplus;
	using namespace gfxinterface;

	int k,i,n=60;
	CPGLColor col;
	CPGLRegionPtr  pRegion (new CPGLRegion);
	pRegion->SetSolidFillColor( 0.8f,0.8f, 1);

	CPGLLinearVectorPtr pX1(new CPGLLinearVector(-60,60.0f/(n-0.9f),n));
	CPGLCircVectorFPtr pY1(new CPGLCircVectorF(n));
	CPGLLinearVectorPtr pX2(new CPGLLinearVector(-60,60.0f/(n-0.9f),n));
	CPGLCircVectorFPtr pY2(new CPGLCircVectorF(n));
	CPGLLinearVectorPtr pX3(new CPGLLinearVector(-60,60.0f/(n-0.9f),n));
	CPGLCircVectorFPtr pY3(new CPGLCircVectorF(n));

	std::vector<float>& vY1 = pY1->GetV();
	std::vector<float>& vY2 = pY2->GetV();
	std::vector<float>& vY3 = pY3->GetV();

	for (i=0;i<n;i++)
	{
		vY1[i]=(float)(80+rand()/(float)RAND_MAX*5*sin((*pX1)[i]/(double)(n-1)*PGL_PI*3));
		vY2[i]=(float)(60+rand()/(float)RAND_MAX*5*sin((*pX1)[i]/(double)(n-1)*PGL_PI*2));
		vY3[i]=(float)(40+rand()/(float)RAND_MAX*5*cos((*pX1)[i]/(double)(n-1)*PGL_PI*2));
	}
	

	CPGLLine2DPtr pLine1(new CPGLLine2D);
	pLine1->SetLayer(LayerFront);
	pLine1->SetLineWidth(2);
	CPGLLine2DPtr pLine2(new CPGLLine2D());
	pLine2->SetLayer(LayerFront);
	pLine2->SetLineWidth(2);
	pLine2->SetInterpolationType( CPGLLine::InterpolationTypeStep );

	CPGLLine2DPtr pLine3(new CPGLLine2D());
	pLine3->SetLayer(LayerFront);
	pLine3->SetLineWidth(2);

	pLine1->SetDatas(pX1,pY1);
	pLine2->SetDatas(pX2,pY2);
	pLine3->SetDatas(pX3,pY3);
	
	pLine1->SetColor(.8f,0,0);
	pLine2->SetColor(0,.8f,0);
	pLine3->SetColor(0,0,.8f);

	pRegion->AddObject(pLine1);
	pRegion->AddObject(pLine2);
	pRegion->AddObject(pLine3);

	CPGLAxe2D* pAxis = pRegion->GetAxe();
	pAxis->SetTitle( _T("Real time trending graph."));
	pAxis->SetBottomLabel( _T("mins") );
	pAxis->GetLeftLabel().Hide();
	pAxis->GetLeftNumber().Hide();
	pAxis->GetRightNumber().Show();
	pAxis->SetSolidFillColor( 0.95f,0.95f, 1);
	pAxis->GetHorGrid().SetDashPattern(_T(""));
	pAxis->GetVerGrid().SetDashPattern(_T(""));
	pAxis->GetHorGrid().SetColor( 0.7f, 0.7f, 1);
	pAxis->GetVerGrid().SetColor( 0.7f, 0.7f, 1);
	pAxis->SetColor(0.7f, 0.7f, 1);

	pAxis->SetAllMainTicksSize(0);
	pAxis->SetAllSecondTicksSize(0);

	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion, CPGLDialog::OptionTripleBuffering);
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);

	// peek and pump...
	CPGLUtility::PeekAndPump();

	k=0;
	while(1)
	{
		k++;
		if (m_pglDlgManager.IsEmpty())
			return;

		pY1->StepStart();
		pY2->StepStart();
		pY3->StepStart();
		
		vY1[ pY1->size()-1 ] = (float)(80+rand()/(float)RAND_MAX*5*sin((*pX1)[i]/(double)(n-1)*PGL_PI*3));
		vY2[ pY2->size()-1 ] = (float)(60+rand()/(float)RAND_MAX*10*sin((*pX1)[i]/(double)(n-1)*PGL_PI*2));
		vY3[ pY3->size()-1 ] = (float)(40+rand()/(float)RAND_MAX*5*cos((*pX1)[i]/(double)(n-1)*PGL_PI*2));

		// applying messages
		CPGLUtility::PeekAndPump();

		// check if dialog is still existing
		if ( !m_pglDlgManager.IsInList(pGraphDlg) )
			return;

		// redraw screen
		pGraphDlg->SetFrontDirty();
	}	
}


void CMainFrame::OnExamplesBargraphs() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleBar(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}

void CMainFrame::OnExamplesSickdataplottingemptylinesnanetc() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleSickData(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();				
}

void CMainFrame::OnExamplesHistogram() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleHistogram(pRegion);
	CPGLAxe2D* pAxis=pRegion->GetAxe();

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();	
}


void CMainFrame::OnExamplesHistogram2() 
{
	{
		CPGLRegionPtr  pRegion (new CPGLRegion);
		CPGLDemo::GetExampleHistogram1(pRegion,1);
		CPGLDialog graphdlg(this, pRegion);
		graphdlg.DoModal();	
	}

	{
		CPGLRegionPtr  pRegion (new CPGLRegion);
		CPGLDemo::GetExampleHistogram1(pRegion,2);
		CPGLDialog graphdlg(this, pRegion);
		graphdlg.DoModal();	
	}

	{
		CPGLRegionPtr  pRegion (new CPGLRegion);
		CPGLDemo::GetExampleHistogram1(pRegion,3);
		CPGLDialog graphdlg(this, pRegion);
		graphdlg.DoModal();	
	}

	{
		CPGLRegionPtr  pRegion (new CPGLRegion);
		CPGLDemo::GetExampleHistogram1(pRegion,50000);
		CPGLDialog graphdlg(this, pRegion);
		graphdlg.DoModal();	
	}
}

void CMainFrame::OnMfcintegrationShowasbasicwindow()
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
	CPGLDemo::GetExampleSimpleLine(pRegion);
	pRegion->GetAxe()->SetTitle(_T("This chart is encapsulated in a CPGLWnd object."));

	m_pglWnd.DestroyRegion();
	m_pglWnd.DestroyWindow();
    
    // remove hook
    if (m_pglWndHooker.IsHooked())
        m_pglWndHooker.UnHook();

	m_pglWnd.SetRegion(pRegion);
	CRect rect;
	CView* pView=GetActiveView();
	pView->GetWindowRect(&rect);
	ScreenToClient(&rect);

	rect.left+=2;
	rect.top+=2;
	rect.right-=2;
	rect.bottom-=2;
	m_pglWnd.Create(NULL,_T("PGLWnd Test"), WS_CHILD | WS_VISIBLE, rect, this, 1234);

    // hook
    m_pglWndHooker.Hook(&m_pglWnd);

	InvalidateRect(NULL,FALSE);
}


void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
	CFrameWnd::OnSize(nType, cx, cy);
	if (m_pglWnd.m_hWnd != NULL)
	{
		CRect rect;
		CView* pView=GetActiveView();
		pView->GetWindowRect(&rect);
		ScreenToClient(&rect);

		rect.left+=2;
		rect.top+=2;
		rect.right-=2;
		rect.bottom-=2;
		m_pglWnd.MoveWindow(rect.left, rect.top, rect.Width(), rect.Height());
	}
}

void CMainFrame::OnExamplesOuputtingtofiles() 
{
	BeginWaitCursor();
	
	CPGLDemo demo;
	demo.GetArchiveDemo(_T("outputTest"));
	
	EndWaitCursor();
}

void CMainFrame::OnGdiGettinginstalledencoderlist() 
{
	using namespace std;
	using namespace gfxinterface;

	vector<_tstring> vEncoders;
	CGDIpHelper::GetImageEncodersList( vEncoders);	

	CString str=_T("Encoders:\r\n");
	for (UINT i=0;i<vEncoders.size();i++)
	{
		str+=vEncoders[i].c_str();
		str+=_T("\r\n");
	}

	AfxMessageBox(str, MB_ICONINFORMATION | MB_OK | MB_APPLMODAL);
}

void CMainFrame::OnGdiInstalleddecoderlist() 
{
	using namespace std;
	using namespace gfxinterface;

	vector<_tstring> vDecoders;
	CGDIpHelper::GetImageDecodersList( vDecoders);	

	CString str=_T("Decoders:\r\n");
	for (UINT i=0;i<vDecoders.size();i++)
	{
		str+=vDecoders[i].c_str();
		str+=_T("\r\n");
	}

	AfxMessageBox(str, MB_ICONINFORMATION | MB_OK | MB_APPLMODAL);
}


void CMainFrame::OnEditCopyasmetafile() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);

	CPGLDemo::GetExampleDashing(pRegion);

	CPGLArchive ar(pRegion);

	ar.ExportToClipBoard(CPGLArchive::ClipBoardMetafile, true, CPGLPointU(640,480), this->m_hWnd);
}

void CMainFrame::OnEditCopyAsbitmap() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);

	CPGLDemo::GetExampleDashing(pRegion);

	CPGLArchive ar(pRegion);

	ar.ExportToClipBoard(CPGLArchive::ClipBoardBitmap, true);
}

void CMainFrame::OnExportingSvgtostring() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);

	CPGLDemo::GetExampleDashing(pRegion);

	CPGLArchive ar(pRegion);

	CString str;
	ar.ExportToString(CPGLArchive::FileFormatSVG, str, CPGLPointU(600,400), true);
}

void CMainFrame::OnExportingOutputtingallexamplestoPng() 
{
	CPGLDemo demo;
	BeginWaitCursor();	
	demo.GetAllExamples( CPGLArchive::FileFormatPNG);
	EndWaitCursor();
}

void CMainFrame::OnExportingOutputtingallexamplestoJpeg() 
{
	CPGLDemo demo;
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatJPG);
	EndWaitCursor();
}

void CMainFrame::OnExportingOutputtingallexamplestoEps() 
{
	CPGLDemo demo;
	
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatEPS);
	EndWaitCursor();
}

void CMainFrame::OnExportingOutputtingallexamplestoEmf() 
{
	CPGLDemo demo;
	
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatEMFPlusDual);
	EndWaitCursor();
}

void CMainFrame::OnExportingOutputtingallexamplestoBmp() 
{
	CPGLDemo demo;
	
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatBMP);
	EndWaitCursor();
}

void CMainFrame::OnExportingOutputtingallexamplestoWmf() 
{
	CPGLDemo demo;
	
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatEMFOnly);
	EndWaitCursor();
}

void CMainFrame::OnExportingOutputtingallexamplestoTiff() 
{
	CPGLDemo demo;
	
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatTIFF);
	EndWaitCursor();
}

void CMainFrame::OnExportingOutputtingallexamplestoSvg() 
{
	CPGLDemo demo;
	
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatSVG);
	EndWaitCursor();
}

void CMainFrame::OnMiscMemoryleakdetection() 
{
	new CPGLLine;	

	AfxMessageBox(_T("A PGL object has been allocated on the heap and not deallocated.\nWatch the trace window for the memory leak..."));
}

void CMainFrame::OnExamplesErrorbars() 
{
	CPGLRegionPtr  pRegion (new CPGLRegion);
		
	CPGLDemo::GetExampleErrorBar(pRegion);

	//Create a dialog box and display the plot:
	CPGLDialog graphdlg(this, pRegion);

	graphdlg.DoModal();		
}

void CMainFrame::OnMiscObjectsinmemory() 
{
	CString str;

#ifdef DEBUG
	CPGLNodePoolSingleton::Dump();
#endif
	size_t nc=CPGLNodePoolSingleton::GetCount();
	size_t oc=CPGLNodePoolSingleton::GetObjectCount();
	size_t dc=CPGLNodePoolSingleton::GetDataCount();
	size_t bc=CPGLNodePoolSingleton::GetBrushCount();
	str.Format(_T("Nodes: %d\nObjects: %d\nData: %d\nBrush: %d\nObject registry: %d object types\n Data registry: %d data types"), 
		nc,
		oc,
		dc,
		bc,
		pgl::CObjectRegistrySingleton::GetCount(),
		pgl::CDataRegistrySingleton::GetCount());	
	AfxMessageBox(str);
}

void CMainFrame::OnMiscLoggingenabled() 
{
	pgl::CLoggerSingleton::SetEnabled( !pgl::CLoggerSingleton::IsEnabled());
}

void CMainFrame::OnUpdateMiscLoggingenabled(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck( pgl::CLoggerSingleton::IsEnabled());
}

void CMainFrame::OnExportingEmftoistream() 
{
	CPGLDemo demo;
	
	demo.GetEmfToIStreamDemo(_T("emf2IStream.emf"));
}

void CMainFrame::OnExportingOutputtingallexamplestoSvgz() 
{
	CPGLDemo demo;
	
	BeginWaitCursor();
	demo.GetAllExamples( CPGLArchive::FileFormatSVGZ);
	EndWaitCursor();	
}

void CMainFrame::OnEditCopy() 
{
	// TODO: Add your command handler code here
	
}


void CMainFrame::OnSpecialLowlevelanimation() 
{
	using namespace std;
	using namespace Gdiplus;
	using namespace gfxinterface;

	
	//*********************************
	// Read the rolling mill data
	//*********************************
	long type = 0;
	long imagf = 0;
	
	CPGLColor col;
	CPGLRegionPtr  pRegion(new CPGLRegion);
	pRegion->SetName(_T("Patterns"));
	pRegion->GetAxe()->SetTitle( _T("hh5 (mm)"));

	pRegion->SetSolidFillColor( (float)1,(float)1, (float)1); // Use 'white' color


	//Create a dialog box and display the plot:
	CPGLDialog* pGraphDlg= new CPGLDialog(this, pRegion, CPGLDialog::OptionNoZoomAll);
	
	CGDIpInterface& gfx = pGraphDlg->GetGfxInterface();   //
	
	CPGLAxe2D* pAxis = pRegion->GetAxe();
	
	CPGLRectD ttt(0,0, (double)7.0,1);
	
	// zooming
	pRegion->ZoomRegion( pGraphDlg->GetGfxInterface(), ttt);
	pAxis->SetAutoSteps( CPGLAxe2D::AxisHorizontal, false);
	
	m_pglDlgManager.RemoveHidden();
	m_pglDlgManager.AddHead(pGraphDlg);

	// peek and pump...
	CPGLUtility::PeekAndPump();

	// add and modify data...
	float x1,y1,x2, y2;
	size_t row=10000;

	for (size_t i=0;i< row-1;++i)
	{
		if (m_pglDlgManager.IsEmpty()) return;

		x1=static_cast<float>(i/(100.0f));
		y1=(static_cast<float>(rand())/RAND_MAX);
		x2=static_cast<float>((i+1)/(100.0f));
		y2=(static_cast<float>(rand())/RAND_MAX);

	{
		// applying messages
		CPGLUtility::PeekAndPump();
		// check if dialog is still existing
		if ( !m_pglDlgManager.IsInList(pGraphDlg) )
			return;

		// getting graphics object associated to memory bitmap
		Graphics* pMemGraphics = pGraphDlg->GetGDIpHelper().GetMemGraphics();


		//! getting client device context of dialog window
		CDC* pDC = pGraphDlg->GetDC();
		if (pMemGraphics && pDC)
		{
			gfx.SetGraphics(pMemGraphics);
			gfx.BeginPaint(pDC->m_hDC);
				// push clipping stuff....
				pRegion->PushWorldClipBBox(gfx);
					gfx.SetColor(1,0,0);
					gfx.DrawLine(x1,y1,x2,y2);
				pRegion->PopWorldClipBBox(gfx);
			gfx.EndPaint();

			pGraphDlg->SetCacheDirty();
			
			// release dc
			pGraphDlg->ReleaseDC(pDC);
			// pumping messages...
			CPGLUtility::PeekAndPump();
		}
		else
			return;
	}
	}
}

void CMainFrame::OnCapiTestmemorybitmap()
{
	CPGLDemo demo;

	demo.TestCAPI();
}

